﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Utils.Common;
using Utils.Common.Enum;
using ZK.BLL;
using ZK.BLL.Config;
using ZK.BLL.MainExecute;
using ZK.Entity;
using ZK.MyControl;
using static Utils.Common.LogHelper;

namespace ZK.Client.WinForm
{
    public partial class FrmCIJExecute : Form
    {
        public FrmCIJExecute()
        {
            InitializeComponent();
            LogHelper.LogEventHandler += LogEventHandler;
            FrmPrintData.SelectedEvent += FrmPrintData_SelectedEvent;
        }

      

        private void LogEventHandler(object sender, EventArgs e)
        {
            LogEntity logEntity = (LogEntity)sender;
            switch (logEntity.LogEnum)
            {
                case LogEnum.Info: { richTextBoxPanel1.LogMessage(logEntity.Message); break; }
                case LogEnum.Debug: { richTextBoxPanel1.LogWarning(logEntity.Message); break; }
                case LogEnum.Error: { richTextBoxPanel1.LogError(logEntity.Message); break; }
            }
        }

        public string currCustomer = string.Empty;
        public string currProductName = string.Empty;
  


        private void GetCurrent()
        {
            currCustomer = cmbCustomer.Text.Trim();
            currProductName = cmbProductName.Text.Trim();
             
        }

        private void FrmCIJExecute_Load(object sender, EventArgs e)
        {
 
 
            btnDoPrint.Enabled = false;
            cmbCustomer.SelectedIndexChanged += CmbCustomer_SelectedIndexChanged;
            string[] customers = ProductInfoBLL.Instance.GetList().Select(s => s.CustomerName).Distinct().ToArray();

            cmbCustomer.DataSource = customers;

            FrmProductInfo.SaveEvent += saveEvent;
 
            if (TempConfig.GetConfig().CustomerName != null)
            {
                cmbCustomer.SelectedIndex= cmbCustomer.FindString(TempConfig.GetConfig().CustomerName);
            }
            if (TempConfig.GetConfig().ProductName != null)
            {
                cmbProductName.SelectedIndex = cmbProductName.FindString(TempConfig.GetConfig().ProductName);
            }
            if (TempConfig.GetConfig().BatchNo != null)
            {
                txtBatchNo.Text = TempConfig.GetConfig().BatchNo;
            }
            if (TempConfig.GetConfig().ModelNo != null)
            {
                txtModelNo.Text = TempConfig.GetConfig().ModelNo;
            }
        }


        private void FrmPrintData_SelectedEvent(string id)
        {
           var printData=  PrintDataBLL.Instance.Get(id);
            if (printData != null)
            {
                cmbCustomer.SelectedIndex = cmbCustomer.FindString(printData.CustomerName);
                cmbProductName.SelectedIndex = cmbProductName.FindString(printData.ProductName);
                txtModelNo.Text = printData.ModelNo;
                txtBatchNo.Text = printData.BatchNo;
            }
        }

        private void CmbCustomer_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            string customer = cmbCustomer.Text.Trim();

            string imgFilePath = ConfigBase.LogoImg + "\\" + customer + ".bmp";
            if (File.Exists(imgFilePath))
            {
                Image img = Image.FromFile(imgFilePath);
                Bitmap imgNew = new Bitmap(img);
                img.Dispose();
                if (pbLogo.Image != null)
                {
                    pbLogo.Image.Dispose();
                }
                pbLogo.Image = imgNew;
            }

            var productInfoList = ProductInfoBLL.Instance.GetList().FindAll(s => s.CustomerName == customer);
            cmbProductName.DataSource = productInfoList;
            cmbProductName.DisplayMember = nameof(ProductInfo.ProductName);
            cmbProductName.ValueMember = nameof(ProductInfo.Id);

            if (productInfoList != null)
            {
                cmbProductName.SelectedIndex = 0;
            }
            GetCurrent();
        }

        private void saveEvent(object sender, EventArgs e)
        {
            string customer = currCustomer;
            string productName = currProductName;


            string[] customers = ProductInfoBLL.Instance.GetList().Select(s => s.CustomerName).Distinct().ToArray();
            cmbCustomer.DataSource = customers;

            cmbCustomer.SelectedIndex = cmbCustomer.FindString(customer);
            cmbProductName.SelectedIndex = cmbProductName.FindString(productName);
        }

        private void btnOpt_Click(object sender, EventArgs e)
        {

            if (PrinterSettingConfig.GetConfig().PrinterSettingList == null || PrinterSettingConfig.GetConfig().PrinterSettingList.Count == 0)
            {
                MessageBox.Show("请先设置喷码机通讯地址！");
                return;

            }
  
            
             string productName = cmbProductName.Text.Trim();

            string[] customers = ProductInfoBLL.Instance.GetList().Select(s => s.CustomerName).Distinct().ToArray();
            if (!customers.Contains(cmbCustomer.Text.Trim())) return;

 


            if (btnOpt.Text == "连接")
            {
                 
                richTextBoxPanel1.TxtClear();

                var printerSetting = PrinterSettingConfig.GetConfig().PrinterSettingList.First() ;

                CIJExecute.Instance.Start(cmbCustomer.Text.Trim(), productName, printerSetting.IPAddress, printerSetting.Port);
                if (CIJExecute.Instance.IsRunning)
                {
                    btnOpt.Text = "断开";
                    btnDoPrint.Enabled = true;

                    LogHelper.LogInfo("连接成功");
                    //DoPrint(GlobalExecute.Instance.GetNewPrintData());
                    
                }
               
            }
            else
            {
                CIJExecute.Instance.Stop();
                if (!CIJExecute.Instance.IsRunning)
                {

                    btnOpt.Text = "连接";
                    btnDoPrint.Enabled = false;
                    LogHelper.LogInfo("连接已断开");
              
                }
            }

           
        }

 
      

    

        private void btnDoPrint_Click(object sender, EventArgs e)
        {

            if (string.IsNullOrEmpty(txtModelNo.Text))
            {

                LogHelper.LogError("请先输入型号号！");
                return;
            }


            string batch = txtBatchNo.Text.Trim();
            if (string.IsNullOrEmpty(batch))
            {

                LogHelper.LogError("请先输入编码！");
                return;
            }

            if (cmbProductName.SelectedIndex == -1)
            {

                LogHelper.LogError("请先选择系列！");
                return;
            }


            var printerSetting = PrinterSettingConfig.GetConfig().PrinterSettingList.First();

            var productInfo  =(ProductInfo) cmbProductName.SelectedItem;
            PrintData printData = new PrintData();
           
            printData.Id = PrintDataBLL.Instance.GetNewID();
            printData.PrinterName = printerSetting.PrinterName;
            printData.CustomerName = cmbCustomer.Text.Trim();
            printData.ProductName = productInfo.ProductName;
            printData.ModelNo= txtModelNo.Text;
            printData.BatchNo = txtBatchNo.Text;
            printData.UserPrintName = Class_Config.UserInfo.PrintName;
            printData.Other = productInfo.Other;
            printData.PersonName = Class_Config.UserInfo.PersonName;
            printData.TemplateName = productInfo.TemplateName;
            printData.PrintDate = DateTime.Now;
            DoPrint(printData);

            try
            {
                TempConfig.GetConfig().CustomerName = cmbCustomer.Text.Trim();
                TempConfig.GetConfig().ProductName = cmbProductName.Text.Trim();
                TempConfig.GetConfig().ModelNo = txtModelNo.Text;
                TempConfig.GetConfig().BatchNo = txtBatchNo.Text.Trim();
                TempConfig.GetConfig().EditConfig();

            }
            catch { }
        }


        private void DoPrint( PrintData printData)
        {
            if (!CIJExecute.Instance.IsRunning) return;
            CIJExecute.Instance.ReSend(printData);
            if (pbImage.Image != null)
            {
                pbImage.Image.Dispose();
            }
           pbImage.Image = CIJExecute.Instance.CreateImage(printData);
        }

        private void pbLogo_Click(object sender, EventArgs e)
        {

            FrmCustomerSelect frm = new FrmCustomerSelect();
            if (frm.ShowDialog() == DialogResult.OK)
            {
                cmbCustomer.SelectedIndex = cmbCustomer.FindString(frm.Selected);
            }
        }

        private void cmbProductName_SelectedIndexChanged(object sender, EventArgs e)
        {
            GetCurrent();
        }
    }
}
